﻿using MengXunAppTool.DAL;
using MengXunAppTool.Helper;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace 软件运行平台.View
{
    /// <summary>
    /// LoginView.xaml 的交互逻辑
    /// </summary>
    public partial class StaionSetView : Window
    {
        public StaionSetView()
        {
            InitializeComponent();
            CbxFolder.ItemsSource = new List<string>() { };
        }
        private VerDAL verDAL = new VerDAL();
        private ConfigHelper configHelper = new ConfigHelper();
        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            configHelper.Write("Customer", CbxCus.Text);
            configHelper.Write("Product", CbxPro.Text);
            if (CbxFolder.Text!="")
            {
                configHelper.Write("LocalDir", CbxFolder.Text);
            }
            this.DialogResult = true;
            this.Close();
        }


        private void main_PreviewKeyDown(object sender, KeyEventArgs e)
        {
             if (e.Key == Key.Escape)
            {
                configHelper.Write("Customer", CbxCus.Text);
                configHelper.Write("Product", CbxPro.Text);
                this.DialogResult = true;
                this.Close();
            }
        }

        private void CbxCus_DropDownOpened(object sender, EventArgs e)
        {
            try
            {
                List<string> source = new List<string>();
                //source.Add("");
                source.AddRange(verDAL.GetCustomers());
                CbxCus.ItemsSource = source;
            }
            catch (Exception ee)
            {
                HandyControl.Controls.Growl.Error(ee.ToString());
                LogHelper.WriteLog(ee.ToString() );
            }
        }

        private void CbxCus_DropDownClosed(object sender, EventArgs e)
        {

        }

        private void CbxPro_DropDownOpened(object sender, EventArgs e)
        {
            try
            {
                if (CbxCus.Text != "")
                {
                    List<string> source = new List<string>();
                    //source.Add("");
                    source.AddRange(verDAL.GetProducts(CbxCus.Text));
                    CbxPro.ItemsSource = source;
                }
            }
            catch (Exception ee)
            {
                HandyControl.Controls.Growl.Error(ee.Message.ToString());
                LogHelper.WriteLog(ee.ToString() );
            }

        }

        private void CbxPro_DropDownClosed(object sender, EventArgs e)
        {

        }

        private void CbxFolder_DropDownOpened(object sender, EventArgs e)
        {
            List<string> source = new List<string>();
            DriveInfo[] drivers = DriveInfo.GetDrives();
            foreach (var driver in drivers)
            {
                if (driver.DriveType != DriveType.Fixed)
                {
                    continue;
                }
                source.Add(driver.Name.Replace(":\\",""));
            }
            CbxFolder.ItemsSource = source;
        }

        private void CbxFolder_DropDownClosed(object sender, EventArgs e)
        {

        }

        private void main_Loaded(object sender, RoutedEventArgs e)
        {
            Dictionary<string, string> dic = configHelper.ReadAll();
            List<string> source = new List<string>() { dic["LocalDir"] };
            CbxFolder.ItemsSource = source;
            CbxFolder.Text = source[0];
        }
    }
}
